package com.test.controller.test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPServer {
    public static void main(String[] args) throws IOException {
        int portNumber = 623; // IPMI默认端口号为623
        try (DatagramSocket serverSocket = new DatagramSocket(portNumber)) {
            byte[] receiveData = new byte[1024];
            byte[] sendData = new byte[1024];
            while (true) {
                DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
                serverSocket.receive(receivePacket);
                String inputLine = new String(receivePacket.getData(), 0, receivePacket.getLength());
                InetAddress IPAddress = receivePacket.getAddress();
                int port = receivePacket.getPort();
                // 在这里处理ipmitool命令
                // 例如，您可以使用Runtime.getRuntime().exec()方法来执行ipmitool命令
                // 并将结果发送回客户端

                System.out.println(inputLine);


                System.out.println(new String(inputLine.getBytes(), "UTF-8"));


                Process process = Runtime.getRuntime().exec(inputLine);
//                String outputLine = new String(process.getInputStream());
//                sendData = outputLine.getBytes();


                sendData = "ok".getBytes();
                DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
                serverSocket.send(sendPacket);
            }
        } catch (IOException e) {
            System.out.println("Exception caught when trying to listen on port " + portNumber + " or listening for a connection");
            System.out.println(e.getMessage());
        }
    }
}